package com.phy.gsjl.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.phy.gsjl.dao.ReplyDAO;
import com.phy.gsjl.dao.UserDAO;
import com.phy.gsjl.entity.Reply;
import com.phy.gsjl.entity.User;
import com.phy.gsjl.service.IReplyService;

/**
 * reply服务层
 * @author curry
 *
 */
@Transactional
@Service
public class ReplyServiceImpl implements IReplyService {

	@Autowired
	private ReplyDAO replyDAO;
	
	@Autowired
	private UserDAO userDAO;
	
	//获取日志对象
	private static Logger logger = Logger.getLogger(ReplyServiceImpl.class);
	
	@Override
	public List<Reply> getAllReplys(Integer m_id) {
		logger.info("==>In ReplySeviceImpl getAllReply parameter m_id: " + m_id);
		List<Reply> replies = replyDAO.getAllReplys(m_id); //根据m_id获取所有的回复
		if (!replies.isEmpty()) {
			//for循环功能是从user表中找到回复人的真实姓名
			for (Reply reply : replies) {
				String username = reply.getUsername(); //回复人的账号
				logger.info("==>In ReplySeviceImpl getAllReply username: " + username);
				User user = userDAO.getUserByName(username); //获取回复人实体 
				//如果回复人在user表中存在
				if (user != null ) {
					String realname = user.getRealname(); //获得回复人的真实姓名
					reply.setUsername(realname); //设置username为真实姓名，传到controller层
				}
				
			}
		}
		return replies;
	}

	@Override
	public boolean addReply(Reply reply) {
		try {
			logger.info("==>In ReplySeviceImpl getAllReply parameter reply: " + reply.toString());
			replyDAO.addReply(reply);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

}
